package com.ten.mall.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.PrivilegeService;
import com.ten.mall.vo.Privilege;
@Controller
public class PrivilegeController {
	@Autowired
	private PrivilegeService pService;
	@RequestMapping("/admin/privilegeList")
	public String userList(@RequestParam(name="start",defaultValue="1") Integer start,
			   				@RequestParam(name="size",defaultValue="10") Integer size,
			   				HttpServletRequest res,String name){
		Map<String,Object> map =pService.selectByPages(start, size, name);
		List<Privilege> list =(List<Privilege>) map.get("list");
		Long mySize=(Long) map.get("total");
		int total = mySize.intValue()/10+1;
		res.setAttribute("list", list);
		res.setAttribute("total", total);
		res.setAttribute("page", start);
		return "jsp/privilegeList";
	}
	
	@RequestMapping("/admin/toPrivilege")
	public String toAdd(Integer id,Model model){
		if(id!=null){
			Privilege p =pService.selectById(id);
			model.addAttribute("p", p);
		}
		return "jsp/privilegeAdd";
	}
	
	@RequestMapping("/admin/savePrivilege")
	@ResponseBody
	public String addUser(Privilege p){
		String rs ="error";
		try {
			if(p.getId()!=null){
				pService.updatePrivilege(p);
			}else{
				pService.insertPrivilege(p);
			}
			rs ="ok";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	@RequestMapping("/admin/delPrivilege")
	@ResponseBody
	public String delPrivilege(Integer id){
		String rs ="ok";
		try {
			pService.deletePrivilege(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rs="error";
		}
		return rs;
	}
}